define(['bootstrap','bootstrapTable','layer','bootstrapTableTreegrid','jqueryTreegrid'],function (bootstrap,bootstrapTable,layer,bootstrapTreegrid,jqueryTreegrid) {
    return{
        init:function () {
            showData();
            /*添加部门*/
            $("#addDepartment").click(function(){
                $("#id").val(0);
                $("#pid").val(0);
                $("#departmentName").val("");
                $("#describes").val("");
                $("#code").val("");
                $("#sort").val("");
                var acheck = $("#bootstrapTable").bootstrapTable('getSelections')[0];
                if(typeof(acheck) != 'undefined') {

                    $("#pname").val(acheck.departmentName);
                    $("#pid").val(acheck.id);
                }else{
                    $("#pname").val("无父级");
                }
                openLayer("修改用户信息",$("#myDepartmentModal"))
            });
            /*关闭*/
            $(".btn_close").click(function(){
                $("#myDepartmentModal").css({"display":"none"})
                layer.closeAll();
            });
            /*保存*/
            $("#btn_save").click(function(){
                var id = $("#id").val();
                var url = "";
                var data = {};
                if(id == 0){ // 新增
                    url = "/departmentController/saveDept";
                    data = {
                        "departmentName":$("#departmentName").val(),
                        "describes":$("#describes").val(),
                        "pId":$("#pid").val(),
                        "code":$("#code").val(),
                        "sort":$("#sort").val()
                    }
                }else { // 修改
                    url = "/departmentController/updateDept";
                    data = {
                        "id":id,
                        "departmentName":$("#departmentName").val(),
                        "describes":$("#describes").val(),
                        "pId":$("#pid").val(),
                        "code":$("#code").val(),
                        "sort":$("#sort").val()
                    }
                }
                $.ajax({
                    url:url,
                    dataType:"json",
                    type:"post",
                    data:data,
                    success:function(result){
                        if(result.code == 10001){
                            layer.msg('保存成功', {icon: 1});
                            layer.closeAll();
                            $('#bootstrapTable').bootstrapTable("refresh");
                        }else{
                            layer.msg('保存失败', {icon: 2});
                        }
                    }
                })

            });
            /*检索*/
            $("#search").click(function(){
                showData();
            })
        }
    };
    function openLayer(title,$dom){
        layer.open({
            type: 1,
            title:title,
            anim: 2,
            closeBtn:1,
            area:["60%","80%"],
            content:$dom,
            cancel: function(index, layero){
                $dom.css({"display":"none"})
            }
        });
    }
    function showData(){
        window.openEvent={
            'click #editor':function (e,value,row,index) {
                $.ajax({
                    url:"/departmentController/findById",
                    dataType:"json",
                    type:"post",
                    data:{
                        "id":row.id
                    },
                    success:function(result){
                        console.log(result);
                        var data = result.data;
                        if(result.code == 10001){
                            openLayer("修改用户信息",$("#myDepartmentModal"));
                            $("#id").val(data.id);
                            if(data.pname === "" || data.pname == null){
                                $("#pname").val('无父级');
                                $("#pid").val(0);
                            }else {
                                $("#pname").val(data.pname);
                                $("#pid").val(data.pId);
                            }
                            $("#sort").val(data.sort);
                            $("#code").val(data.code);
                            $("#departmentName").val(data.departmentName);
                            $("#describes").val(data.describes);
                        }
                    }
                })

            },
            'click #delete':function (e,value,row,index) {
                layer.confirm('您确定要删除此部门？', {title:'删除信息',icon: 2,
                    btn: ['确定','取消'] //按钮
                }, function(){
                    $.ajax({
                        url:"/departmentController/deleteDdpt",
                        dataType:"json",
                        type:"post",
                        data:{
                            "id":row.id
                        },
                        success:function(result){
                            if(result.code == 10001){
                                layer.msg('删除成功', {icon: 1});
                                layer.closeAll();
                                $('#bootstrapTable').bootstrapTable("refresh");
                            }
                        }
                    })
                    layer.close();
                });
            }
        }
        $('#bootstrapTable').bootstrapTable({
            url:'/departmentController/pageSelect',
            method:'post',
            sidePagenation:'server',
            idField:'id',
            dataType:"json",
            cache: false,       // 设置为 false 禁用 AJAX 数据缓存， 默认为true
            contentType:'application/x-www-form-urlencoded',
            //striped: true,      //表格显示条纹，默认为false
            pagination: true,   // 在表格底部显示分页组件，默认false
            pageList: [20,50,100], // 设置页面可以显示的数据条数
            pageSize: 30,       // 页面数据条数
            pageNumber: 1,      // 首页页码
            queryParamsType : "",
            queryParams:function( ) {
                return tem = {
                    departmentName: $("#name").val()
                }
            },
            columns:[
                {
                    field: 'ck',
                    checkbox: true,
                    width:'1%'
                },{
                    field:'departmentName',
                    align: 'center',
                    valign: 'middle',
                    title:'部门名称',
                    width:'20%'
                },{
                    field:'code',
                    align: 'center',
                    valign: 'middle',
                    title:'部门编号',
                    width:'20%'
                },{
                    field:'describes',
                    align: 'center',
                    valign: 'middle',
                    title:'描述',
                    width:'20%'
                },{
                    field:'createUser',
                    align: 'center',
                    valign: 'middle',
                    title:'创建人',
                    width:'20%'
                },{
                    field:'operator',
                    align: 'center',
                    valign: 'middle',
                    title:'操作',
                    events:openEvent,
                    width:'20%',
                    formatter:function(value,row,index){
                        return ['<button class="btn btn-xs btn-info btn-operator-gew" id="editor"><i class="fa fa-edit"></i></button>','<button class="btn btn-xs btn-danger" id="delete"><i class="fa fa-trash-o"></i></button>'].join("");
                    }
                }
            ],
            treeShowField: 'departmentName',
            parentIdField: 'pId',
            onLoadSuccess: function(data) {
                $("#bootstrapTable").treegrid({
                    initialState: 'collapsed',//收缩
                    treeColumn: 1,//指明第几列数据改为树形
                    expanderExpandedClass: 'fa fa-chevron-circle-down',
                    expanderCollapsedClass: 'fa fa-chevron-circle-right',
                    onChange: function() {

                        $("#bootstrapTable").bootstrapTable('resetWidth');
                    }
                });
                //$('#bootstrapTree').bootstrapTable("refresh");
            }
        });
    };
    function showNumber(value, row, index){
        var options = $('#bootstrapTable').bootstrapTable('getOptions');
        return options.pageSize*(options.pageNumber-1)+index + 1;
    }

    //时间戳转换
    function fmtDate(obj){
        if(obj == null){
            return null
        }
        var date = new Date(obj);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        Y = date.getFullYear() + '-';
        M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
        D = (date.getDate()< 10 ? '0'+(date.getDate()): date.getDate()) + ' ';
        h = (date.getHours()< 10 ? '0'+(date.getHours()): date.getHours()) + ':';
        m = (date.getMinutes()< 10 ? '0'+(date.getMinutes()): date.getMinutes()) + ':';
        s = (date.getSeconds()< 10 ? '0'+(date.getSeconds()): date.getSeconds());
        return Y+M+D+h+m+s;
    }


})